<?php
/**
 * Author: 周斌斌 <553193368@qq.com>
 * Date: 17/3/21 PM3:31
 * 摘要:
 */

namespace app\shop\controller\v1;
use org\BaiduMap;
use think\Db;
use think\Exception;
use think\Controller;

class Common extends Controller
{

    // public function _initialize()
    // {
    //     parent::_initialize();
    // }


    // 上传图片
    public function upload()
    {
            $param = input('post.');
            // $rs = upload($param['name'], request()->file($param['name']));
            $file = request()->file($param['name']);
            $info = $file->validate(['size' => 111135678, 'ext' => 'jpg,png,gif,jpeg'])->rule('')->move(ROOT_PATH . 'public' . DS . 'uploads/seller/image/', md5('image' . date('Ymd') . time() . rand(1, 10000)) . '.png');
            if ($info) {
                $file = ROOT_PATH . 'public' . DS . 'uploads/seller/image/' . $info->getSaveName();
                $res = alioss('uploads/seller/image/' . $info->getSaveName(), $file);
                $path = config('SELLER_PATH') . $info->getSaveName();
                if ($res) {
                    $rs['data']['rawUrl_path'] = $res;
                    $rs['data']['rawUrl'] = $path;
                }
            }
            if($rs){
                return ['code'=>200,'msg'=>'上传成功','data'=>$rs['data'],'status'=>true];
            }else{
                return ['code'=>400,'msg'=>'上传成功','data'=>$rs['data'],'status'=>false];
            }
    }

    public function getArea()
    {
        $list = Db::name('extend_areas')->where('parent_id',$_POST['id'])->select();
        returnApi(true, '列表返回成功', $list);
    }

    // 获取区域
    public function getAreas()
    {
        try {
            $res = Db::name('extend_areas')->where([
                'region_type' => ['<>', 0],
                'area_type' => 1,
            ])->select();
            $areaArr = [];
            if (!empty($res)) {
                foreach ($res as $k => $v) {
                    $areaArr[$this->areaName[$v['region_type']]][] = $v;
                }
            }
            unset($areaArr['台港澳地区']);
            returnApi(true, '列表返回成功', $areaArr);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    // 获取区域
    public function getAreasIndex()
    {
        try {
            $res = Db::name('extend_areas')->where([
                'region_type' => ['<>', 0],
                'area_type' => 1,
            ])->select();
            $areaArr = [];
            if (!empty($res)) {
                foreach ($res as $k => $v) {
                    $areaArr[$v['region_type']][] = $v;
                }
            }
            $newArr = [];
            foreach ($areaArr as $k => $v) {
                $newArr[] = $v;
            }
            returnApi(true, '列表返回成功', $newArr);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }

    public function authPush()
    {
        try {
            $driverId = input('post.driverId');
            if (empty($driverId)) {
                throw new Exception('司机id必传');
            }
            $type = input('post.type');
            if (empty($type)) {
                throw new Exception('类型必传');
            }
            $driverInfo = Db::name('driver')->where('driverId', $driverId)->find();
            if (empty($driverInfo)) {
                throw new Exception('司机信息为空');
            }
            $reason = Db::name('driver_auth_refuse_reason')->where('did', $driverId)->find();
            $res = sendPush([
                'type' => 'd',
                'Content' => '您有新的认证信息',
                'Title' => '您有新的认证信息',
                'alias' => $driverInfo['alias'],
                'data' => [
                    'code' => '2417',
                    'sound' => '您有新的认证信息',
                    'content' => '您有新的认证信息',
                    'data' => [
                        'type' => $type, //  1是司机,2是快递
                        'reason' => isset($reason['reason']) && !empty($reason['reason']) ? $reason['reason'] : '无备注',
                        'authStatus' => $driverInfo['authStatus'],
                        'expressAuthStatus' => $driverInfo['expressAuthStatus']
                    ],
                    'title' => '您有新的认证信息'
                ],
                'extras' => []
            ]);
            px($res);
        } catch (Exception $e) {
            returnApi(false, $e->getMessage());
        }
    }
}